package com.yuyou.fn.crm.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.crm.entity.CrmDepartment;
import com.yuyou.fn.crm.entity.CrmUserDepartment;
import com.yuyou.fn.crm.mapper.CrmUserDepartmentMapper;
import com.yuyou.fn.crm.service.ICrmDepartmentService;
import com.yuyou.fn.crm.service.ICrmUserDepartmentService;
import com.yuyou.fn.crm.vo.CrmUserDepartSaveUpdateVo;
import com.yuyou.fn.crm.vo.CrmUserDepartmentDetailVo;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.platform.service.ISysUserService;
import com.yuyou.fn.platform.vo.SysUserQueryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * CrmUserDepartment 表数据服务层接口实现类
 *
 */
@Service
public class CrmUserDepartmentServiceImpl extends SuperServiceImpl<CrmUserDepartmentMapper, CrmUserDepartment,Long> implements ICrmUserDepartmentService {

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ICrmDepartmentService crmDepartmentService;

    @Override
    public void saveOrUpdateUserDepart(CrmUserDepartSaveUpdateVo crmUserDepartSaveUpdateVo) {
        Principals.Principal principal=Principals.get();
        if (principal==null){
            throw new BusException("请登录");
        }
        CrmUserDepartment crmUserDepartment=new CrmUserDepartment();
        BeanUtils.copyProperties(crmUserDepartment,crmUserDepartSaveUpdateVo);
        crmUserDepartment.setUpdateTime(TimeUtils.now());
        crmUserDepartment.setOpeartorName(principal.name);
        crmUserDepartment.setOperatorId(principal.id);
        if (crmUserDepartment.getUserDepartmentId()!=null){
            this.updateSelectiveById(crmUserDepartment);
        }else {
            crmUserDepartment.setCreateTime(TimeUtils.now());
            this.insert(crmUserDepartment);
        }
    }

    @Override
    public Page<CrmUserDepartmentDetailVo> findUserDepartmentDetailPage(SysUserQueryVo sysUserQueryVo, int pageNo, int pageSize) {
        Page<SysUser> page = sysUserService.findSysUserPageByQuery(sysUserQueryVo, pageNo, pageSize);
        List<SysUser> sysUserList=page.getRecords();
        Page<CrmUserDepartmentDetailVo> crmUserDepartmentDetailVoPage= Utils.newPage(page);
        List<Long> userIds = BeanUtils.toList(sysUserList, "userId");
        List<CrmUserDepartment> crmUserDepartmentList=this.findUserDepartByUserIds(userIds);
        Map<Long, CrmUserDepartment> userDepartmentMap = BeanUtils.toMap(crmUserDepartmentList, "userId");
        List<Long> departmentIds = BeanUtils.toList(crmUserDepartmentList, "departmentId");
        departmentIds=Utils.removeDuplicate(departmentIds);
        List<CrmDepartment> crmDepartmentList=crmDepartmentService.findByIds(departmentIds);
        Map<Long, CrmDepartment> departmentMap = BeanUtils.toMap(crmDepartmentList, "departmentId");

        List<CrmUserDepartmentDetailVo> crmUserDepartmentDetailVoList=new ArrayList<>();
        for (SysUser sysUser:sysUserList){
            CrmUserDepartmentDetailVo crmUserDepartmentDetailVo=new CrmUserDepartmentDetailVo();
            BeanUtils.copyProperties(crmUserDepartmentDetailVo,sysUser);
            CrmUserDepartment crmUserDepartment=userDepartmentMap.get(sysUser.getUserId());
            if (crmUserDepartment!=null){
                crmUserDepartmentDetailVo.setUserDepartmentId(crmUserDepartment.getUserDepartmentId());
                CrmDepartment crmDepartment=departmentMap.get(crmUserDepartment.getDepartmentId());
                if (crmDepartment!=null){
                    crmUserDepartmentDetailVo.setDepartmentId(crmDepartment.getDepartmentId());
                    crmUserDepartmentDetailVo.setDepartmentName(crmDepartment.getDepartmentName());
                }
            }
            crmUserDepartmentDetailVoList.add(crmUserDepartmentDetailVo);
        }

        crmUserDepartmentDetailVoPage.setRecords(crmUserDepartmentDetailVoList);
        return crmUserDepartmentDetailVoPage;
    }

    @Override
    public List<CrmUserDepartment> findUserDepartByUserIds(List<Long> userIds) {
        Example<CrmUserDepartment> crmUserDepartmentExample=CrmUserDepartment.createExample();
        crmUserDepartmentExample.createCriteria().andIn("userId",userIds);
        return findListByExample(crmUserDepartmentExample);
    }

    @Override
    public CrmUserDepartment findUserDepartByUserId(Long userId) {
        Example<CrmUserDepartment> crmUserDepartmentExample=CrmUserDepartment.createExample();
        crmUserDepartmentExample.createCriteria().andEqualTo("userId",userId);
        return findOneByExample(crmUserDepartmentExample);
    }

}